// $codepro.audit.disable assignmentToNonFinalStatic
/**
 * FileComment: initial session before rank is verified
 */
package edu.gatech.oad.project.networking.clientSessions; // $codepro.audit.disable packageNamingConvention

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import edu.gatech.oad.project.operations.users.User;
import edu.gatech.oad.project.presentation.DriverMain;


/**
 * Represents the initial connection with the server.
 * 
 * @author James Moore
 * @version 1.5
 */
public class loginSession implements Runnable { // $codepro.audit.disable classNamingConvention
    
    /**
     * Field username: user's username
     */
    private final String username;
    
    /**
     * Field password: the user's password
     */
    private final String password;
    
    /**
     * Field login: true if attempting to login, false if registering
     */
    private final boolean login;
    
    /**
     * Field rank: rank of the user
     */
    private final String rank;
    
    /**
     * Field myLogger: holds the logget for the clients file
     */
    private static final Logger MYLOGGER = Logger.getLogger("edu.gatech." +
            "oad.project.presentation");
    
    /**
     * Default Constructor
     * 
     * @param username of the user
     * @param password of the user
     * @param rank of the user
     * @param login designates loging in or registering
     */
    public loginSession(String username, String password, String rank, boolean login) {
        this.username = username;
        this.password = password;
        this.login = login;
        this.rank = rank;
        DriverMain.inBound = null;
        DriverMain.outBound = null;
        
        MYLOGGER.addHandler(DriverMain.FILEHANDLER);
        MYLOGGER.setLevel(Level.ALL);
    }
    
    /**
     * Initializes when thread runs
     * 
     * @see java.lang.Runnable#run()
     * 
     */
    @Override
    public void run() {
        synchronized(DriverMain.loginData) {

            final String host = "localhost";
            final int port = 1337; // $codepro.audit.disable numericLiterals
            
            try {
                    DriverMain.server = new Socket("localhost", port); // $codepro.audit.disable closeWhereCreated
                    DriverMain.outBound = new ObjectOutputStream( // $codepro.audit.disable closeWhereCreated
                            DriverMain.server.getOutputStream());
                    DriverMain.outBound.flush();
                    DriverMain.inBound = new ObjectInputStream( // $codepro.audit.disable closeWhereCreated
                            DriverMain.server.getInputStream());
            } catch (UnknownHostException e) {
                    MYLOGGER.severe("Don't know about host:" + host + e);
            } catch (IOException e) {
                    MYLOGGER.severe("Couldn't get I/O for the connection to:" + host + e);
            }
            if (login) {    
                try {
                    DriverMain.outBound.writeUnshared("login");
                    DriverMain.outBound.writeUnshared(new String(username));
                    DriverMain.outBound.writeUnshared(new String(password));
                    DriverMain.outBound.flush();
                
                    final String validate = (String) DriverMain.inBound.readUnshared();
                    final String num = (String) DriverMain.inBound.readUnshared();
                    final String rankError = (String) DriverMain.inBound.readUnshared();
                
                    System.out.println(validate);
                    System.out.println(num);
                    System.out.println(rankError);
                
                    DriverMain.loginData = new String[] {validate, num, rankError};
                
                    if (DriverMain.loginData[0].equals("true")) {
                        DriverMain.token = (Long) DriverMain.inBound.readUnshared();
                        DriverMain.user = (User) DriverMain.inBound.readUnshared();
                        
                    }
                    
                    //DriverMain.outBound.close();
                    //DriverMain.inBound.close();
                
                } catch (IOException e) {
                    MYLOGGER.severe("" + e);
                    //e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    MYLOGGER.severe("" + e);
                    //e.printStackTrace();
                } catch (Exception e) {
                    MYLOGGER.severe("" + e);
                    //e.printStackTrace();
                }

            }
            else {
                synchronized(DriverMain.register) {
                    try {
                        DriverMain.outBound.writeUnshared("register");
                        DriverMain.outBound.writeUnshared(new String(username));
                        DriverMain.outBound.writeUnshared(new String(password));
                        DriverMain.outBound.writeUnshared(new String(rank));
                        DriverMain.outBound.flush();
                        
                        DriverMain.register = (Boolean) DriverMain.inBound.readUnshared();
              
                    } catch (IOException e) {
                        MYLOGGER.severe("" + e);
                        //e.printStackTrace();
                    } catch (ClassNotFoundException e) {
                        MYLOGGER.severe("" + e);
                        //e.printStackTrace();
                    }

                }
            }
        }
    
    }
    
    /** (non-Javadoc)
     * @see java.lang.Object#toString()
     * @return a string
     */
    @Override
    public String toString() {
        return "loginSession [username=" + username + ", password=" + password
                + ", login=" + login + ", rank=" + rank + "]";
    }
    
}
